﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Entities;
using DAL;

namespace SurveyGUI
{
    public partial class AdministratorFrom : Form
    {
        //khai bao doi tuong ket noi
        //SqlConnection connect = new SqlConnection(@"server = TRUONG-PC\SQLEXPRESS; database = Nhom15; integrated security = true;");
        static string str = "Data Source=TRUONG-PC\\SQLEXPRESS;Initial Catalog=Nhom15;Integrated Security=True";
        SqlConnection connect = new SqlConnection(str);
        //doi tuong de doc du lieu bang
        SqlDataAdapter da;
        //
        DataSet ds;
        IInspectRepository rep = new InspectRepository();
        //IAccountRepository repAccount = new AccountRepository();

        public AdministratorFrom()
        {
            InitializeComponent();
        }

        ////lay ten user admin de gan vao textbox Ten dang nhap cua form ChangPassAdmin
        //public string tendn
        //{
        //    get { return lblUser.Text; }
        //}

        //lay ten dang nhap admin gan len form admin
        public string tenAD
        {
            set { lblUser.Text = value; }
        }

        private void AdministratorFrom_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        //Load Du Lieu
        private void LoadData()
        {
            //khoi tao doi tuong doc du lieu
            da = new SqlDataAdapter("select * from ThanhTra", connect);
            ds = new DataSet();
            //doi tuong DataTable de luu du lieu
            //DataTable table = new DataTable();
            //dien du lieu vao DataTable
            //da.Fill(table);
            //dgvDanhSach.DataSource = table;
            try
            {
                //Do du lieu vao DataSet, select tu bang ThanhTra
                da.Fill(ds, "ThanhTra");
                dgvDanhSach.DataSource = ds.Tables["ThanhTra"];
            }
            catch
            {
                
            }
        }

        //Them thanh tra
        private void btnThem_Click(object sender, EventArgs e)
        {
            AddInspectForm addIn = new AddInspectForm();
            addIn.ShowDialog();
            LoadData();
        }

        //Sua thong tin thanh tra
        #region Sua thong tin thanh tra
        private void btnSua_Click(object sender, EventArgs e)
        {
            if (lblTest.Text != "")
            {
                int ma = int.Parse(lblTest.Text);
                UpdateInspectForm update = new UpdateInspectForm(ma, ten, gt, cv, tdn, mk, 2);
                update.ShowDialog();
                LoadData();
            }
            else
                MessageBox.Show("Chọn một thanh tra để sửa!");
        }

        //Lay thong tin thanh tra duoc chon va gan len form Update
        string ten;
        string gt;
        string cv;
        string tdn;
        string mk;
        //int kt;

        #endregion

        //CellClick
        private void dgvDanhSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lblTest.Text = dgvDanhSach.CurrentRow.Cells[0].Value.ToString();
            //ma = int.Parse(dgvDanhSach.CurrentRow.Cells[0].Value.ToString());
            ten = dgvDanhSach.CurrentRow.Cells[1].Value.ToString();
            cv = dgvDanhSach.CurrentRow.Cells[2].Value.ToString();
            gt = dgvDanhSach.CurrentRow.Cells[3].Value.ToString();
            tdn = dgvDanhSach.CurrentRow.Cells[4].Value.ToString();
            mk = dgvDanhSach.CurrentRow.Cells[5].Value.ToString();
            //kt = int.Parse(dgvDanhSach.CurrentRow.Cells[6].Value.ToString());
        }

        //Xoa thanh tra
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lblTest.Text != "")
            {
                int id = int.Parse(lblTest.Text);
                if (id > 0)
                {
                    Inspect i = rep.GetById(id);
                    rep.Delete(i);

                    //DialogResult dlg = MessageBox.Show("Xóa thanh tra thành công. Xóa tài khoản của thanh tra này", "Xóa tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    //if (dlg == System.Windows.Forms.DialogResult.OK)
                    //{
                    //Lay thanh tra theo ten va xoa tai khoan thanh tra nay
                    //Account a = repAccount.GetByName(i.TenTT);
                    //repAccount.DeleteAccount(a);
                    //MessageBox.Show("Xóa thành công");
                    LoadData();
                    //this.Close();
                    //}
                }
            }
            else
                MessageBox.Show("Chọn một thanh tra để xóa");
        }

        //Doi mat khau Admin
        private void btnDoiMK_Click(object sender, EventArgs e)
        {
            ChangePassAdminForm change = new ChangePassAdminForm();
            change.tenUserAdmin = lblUser.Text;
            change.ShowDialog();
        }

        //Tim kiem thanh tra theo ten
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            //Su dung DataView: cho phep loc, sap xep, tim kiem du lieu va do du lieu vao DataGridView de hien thi
            //Dung doi tuong DataSet lay du lieu len
            if (txtTimKiem.Text != "")
            {
                DataView dataView = new DataView(ds.Tables["ThanhTra"]);
                dgvDanhSach.DataSource = dataView;
                dataView.RowFilter = "Ten like '" + txtTimKiem.Text + "%' ";
            }
            else
            {
                dgvDanhSach.DataSource = ds.Tables["ThanhTra"];
            }
        }

        //ContextMenu
        private void dgvDanhSach_MouseClick(object sender, MouseEventArgs e)
        {
            dgvDanhSach.ContextMenuStrip = contextMenuStrip1;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
